/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tParam.type = -1;
  self.$ySpliceAll(tiyData.tData.sysSizeList);
};

function modalActionLoading(self, tiyData) {
  tiyData.tStatus.modalAction.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalAction.isLoading = true;
  });
}

/**
 * 请求数据
 * @param self
 * @param tiyData
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.sysSizeList);
  self.$apiProduct.listSysSizes({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "type": tiyData.tParam.type
  }).then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        let sysSizePage = response.result.sysSizePage;
        tiyData.tPage.total = sysSizePage.total;
        tiyData.tPage.stepNumber = sysSizePage.pageSize;
        if (0 == sysSizePage.list.length) {
          return;
        }
        sysSizePage.list.forEach(function (item) {
          tiyData.tData.sysSizeList.push(item);
        });
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

/**
 * 显示维护系统尺码信息窗口
 * @param self
 * @param tiyData
 */
export const showSysSize = (self, tiyData, isAdd, index) => {
  tiyData.tStatus.modalModify.isLoading = true;
  tiyData.tStatus.modalModify.isShow = true;
  if(isAdd){
    tiyData.tParam.paramSysSize.id='',
    tiyData.tParam.paramSysSize.type=0,
    tiyData.tParam.paramSysSize.name='',
    tiyData.tParam.paramSysSize.sequence=0,
    tiyData.tStatus.modalAction.isAdd = true
    // self.$refs['paramSysSize'].resetFields();
  } else {
    let sysSize = tiyData.tData.sysSizeList[index];
    tiyData.tParam.paramSysSize.id = sysSize.id,
    tiyData.tParam.paramSysSize.type = sysSize.type,
    tiyData.tParam.paramSysSize.name = sysSize.name,
    tiyData.tParam.paramSysSize.sequence = sysSize.sequence,
    tiyData.tStatus.modalAction.isAdd = false
  }

};

/**
 * 保存系统尺码信息
 * @param self
 * @param tiyData
 * @param index
 */
export const saveSysSize = (self, tiyData, name) => {
  tiyData.tStatus.modalModify.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      var param = {
        id: tiyData.tParam.paramSysSize.id,
        type:tiyData.tParam.paramSysSize.type,
        name: tiyData.tParam.paramSysSize.name,
        sequence:tiyData.tParam.paramSysSize.sequence
      };
      self.$apiProduct.saveSysSize(param)
        .then(function (response) {
          //changeModelLoading(self, tiyData);
          tiyData.tStatus.modalModify.isLoading = false;
          if (response.success) {
            self.$Message.success('保存成功!');
            tiyData.tStatus.modalModify.isShow = false;
            webList(self, tiyData);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      tiyData.tStatus.modalModify.isLoading = false;
    }
  });
};

/**
 * 删除系统尺码信息
 * @param self
 * @param tiyData
 * @param index
 */
export const showDelSysSizeModal = (self, tiyData, index) => {
  let sysSize = tiyData.tData.sysSizeList[index];
  tiyData.tStatus.modalAction.sysSize.id = sysSize.id;
  tiyData.tStatus.modalAction.sysSize.name = sysSize.name;
  tiyData.tStatus.modalAction.content = "是否删除尺码（"+sysSize.name+"），删除后无法找回";
  tiyData.tStatus.modalAction.isShow = true;
};

/**
 * 删除系统尺码信息
 * @param self
 * @param tiyData
 * @param index
 */
export const deleteSysSize = (self, tiyData) => {
  var param = {
    id: tiyData.tStatus.modalAction.sysSize.id,
    name: tiyData.tStatus.modalAction.sysSize.name,
  };
  self.$apiProduct.deleteSysSize(param)
    .then(function (response) {
      modalActionLoading(self, tiyData);
      tiyData.tStatus.modalModify.isLoading = false;
      if (response.success) {
        self.$Message.success('删除成功!');
        tiyData.tStatus.modalAction.isShow = false;
        webList(self, tiyData);
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};
